var app = getApp();
var time = app.globalData.shi;
var SocketTask;
var socketOpen = false;
Page({
  data: {
    lyb:"最帅",
    lyk:"gengshuai",
    title: "ddemo",
    currentIndex: 0,
    oldIndex: 0,
    view: [{
      in: "",
      out: ""
    }, {
      in: "",
      out: ""
    }],
    time: time,
  },
  //成功提示弹窗
  popSuccessTest: function () {
    wx.showToast({
      title: '成功提示弹窗',
      icon: '',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
      duration: 2000,      //停留时间
    })
  },

  ppp123: function () {
    wx.navigateTo({
      url: '../title/title?name=1',
    })
    console.log(1)
  },
  kkk123: function () {
    console.log(1)
    wx.navigateTo({
      url: '../title/title?name=2',
    })
  },
  cance: function () {
    wx.redirectTo({
      url: '../login/login',
    })
  },
  //浇水
  jiaoshui:function(){
    if (socketOpen) {
      // 如果打开了socket就发送数据给服务器
      SocketTask.send({
        data: app.heard + "," + app.uid + ",1"
      }, function (res) {
        console.log('已发送', res)
      })
      wx.showToast({
        title: '操作成功',
        icon: '',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 1000,      //停留时间
      })
    }else{
      console.log('webSocket断开了')
      wx.showToast({
        title: '操作失败',
        icon: 'none',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 2000,      //停留时间
      })
    }
  },
  //通过 WebSocket 连接发送数据，需要先 wx.connectSocket，并在 wx.onSocketOpen 回调之后才能发送。
  //开灯
  kaideng: function () {
    if (socketOpen) {
      // 如果打开了socket就发送数据给服务器
      SocketTask.send({
        data: app.heard + "," + app.uid + ",2"
      }, function (res) {
        console.log('已发送', res)
      })
      wx.showToast({
        title: '操作成功',
        icon: '',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 1000,      //停留时间
      })
    } else {
      console.log('webSocket断开了')
      wx.showToast({
        title: '操作失败',
        icon: 'none',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 2000,      //停留时间
      })
    }
  },
  //施肥
  shifei: function () {
    if (socketOpen) {
      // 如果打开了socket就发送数据给服务器
      SocketTask.send({
        data: app.heard + "," + app.uid + ",3"
      }, function (res) {
        console.log('已发送', res)
      })
      wx.showToast({
        title: '操作成功',
        icon: '',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 1000,      //停留时间
      })
    } else {
      console.log('webSocket断开了')
      wx.showToast({
        title: '操作失败',
        icon: 'none',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 2000,      //停留时间
      })
    }
  },
  touchStart: function (t) {
    this.setData({
      startX: t.changedTouches[0].clientX
    })
  },
  touchEnd: function (t) {
    var e = this,
      n = this.data.view;
    this.setData({
      endX: t.changedTouches[0].clientX
    });
    var a = t.changedTouches[0].clientX - this.data.startX;
    if (a < -100) {
      if (this.data.currentIndex >= 1) return;
      this.setData({
        oldIndex: e.data.currentIndex,
        currentIndex: ++e.data.currentIndex
      }), n[this.data.oldIndex].out = "animated fadeOutLeft", n[this.data.oldIndex].in = "", n[this.data.currentIndex].in = "animated fadeInRight", n[this.data.currentIndex].out = "", this.setData({
        view: n
      }), this.cleanAnimated(), this.showAnimated()
    } else if (a > 100) {
      if (this.data.currentIndex <= 0) return;
      this.setData({
        oldIndex: e.data.currentIndex,
        currentIndex: --e.data.currentIndex
      }), n[this.data.oldIndex].out = "animated fadeOutRight", n[this.data.oldIndex].in = "", n[this.data.currentIndex].in = "animated fadeInLeft", n[this.data.currentIndex].out = "", this.setData({
        view: n
      }), this.cleanAnimated(), this.showAnimated()
    }
  },
  showAnimated: function () {
    var t = this;
    0 === this.data.currentIndex ? (setTimeout(function () {
      t.setData({
        one_one: "animated fadeIn",
        one_two: "animated bounceIn"
      })
    }, 1e3), setTimeout(function () {
      t.setData({
        one_three: "animated bounceIn"
      })
    }, 1500), setTimeout(function () {
      t.setData({
        one_four: "animated bounceIn"
      })
    }, 1800), setTimeout(function () {
      t.setData({
        one_five: "animated lightSpeedIn"
      })
    }, 1900)) : 1 === this.data.currentIndex && (setTimeout(function () {
      t.setData({
        two_one: "animated fadeInDown",
        two_two: "animated fadeInUp"
      })
    }, 1e3), setTimeout(function () {
      t.setData({
        two_three: "animated zoomIn",
        two_four: "animated zoomIn"
      })
    }, 1200), setTimeout(function () {
      t.setData({
        two_three: "two-music-one",
        two_four: "two-music-two"
      })
    }, 2200))
  },
  cleanAnimated: function () {
    0 === this.data.oldIndex ? this.setData({
      one_one: "animated fadeOut",
      one_two: "animated fadeOut",
      one_three: "animated fadeOut",
      one_four: "animated fadeOut",
      one_five: "animated fadeOut"
    }) : 1 === this.data.oldIndex && this.setData({
      two_one: "animated fadeOut",
      two_two: "animated fadeOut",
      two_three: "animated fadeOut",
      two_four: "animated fadeOut",
      two_five: "animated fadeOut"
    })
  },
  //
  onLoad: function () { 
    
  },
  //
  onReady: function () {
    //心跳效果
    var circleCount = 0;
    // 心跳的外框动画 
    this.animationMiddleHeaderItem = wx.createAnimation({
      duration: 1000, // 以毫秒为单位 
      /** 
      * http://cubic-bezier.com/#0,0,.58,1 
      * linear 动画一直较为均匀 
      * ease 从匀速到加速在到匀速 
      * ease-in 缓慢到匀速 
      * ease-in-out 从缓慢到匀速再到缓慢 
      * 
      * http://www.tuicool.com/articles/neqMVr 
      * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过 
      * step-end 保持 0% 的样式直到动画持续时间结束  一闪而过 
      */
      timingFunction: 'linear',
      delay: 100,
      transformOrigin: '50% 50%',
      success: function (res) {
      }
    });
    setInterval(function () {
      if (circleCount % 2 == 0) {
        this.animationMiddleHeaderItem.scale(1.15).step();
      } else {
        this.animationMiddleHeaderItem.scale(1.0).step();
      }
      this.setData({
        animationMiddleHeaderItem: this.animationMiddleHeaderItem.export()
      });
      circleCount++;
      if (circleCount == 1000) {
        circleCount = 0;
      }
    }.bind(this), 1000);

    //websocket连接
    var that = this;
    SocketTask.onOpen(res => {
      socketOpen = true;
      console.log('监听 WebSocket 连接打开事件。', res)
    })
    SocketTask.onClose(onClose => {
      console.log('监听 WebSocket 连接关闭事件。', onClose)
      socketOpen = false;
      this.webSocket()
    })
    SocketTask.onError(onError => {
      console.log('监听 WebSocket 错误。错误信息', onError)
      socketOpen = false
      wx.showToast({
        title: '连接错误',
        icon: 'none',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 2000,      //停留时间
      })
    })
    SocketTask.onMessage(onMessage => {
      console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage.data)
      //var onMessage_data = JSON.parse(onMessage.data)
    })
  },
  webSocket: function () {
    var that = this;
    // 创建Socket
    SocketTask = wx.connectSocket({
      url: app.wxSocket,
      // header: {
      //   'content-type': 'application/text'
      // },
      method: "GET",
      success() {
        console.log('success', app.heard);
        that.setData({
          message: "ws连接成功"
        })
        
         wx.onSocketOpen(function () {
           socketOpen = true
          wx.sendSocketMessage({
            // data: JSON.stringify({ id: '10002' }),
            data: app.heard + ",1,1",
          })
          wx.onSocketMessage(function (res) {
            console.log("收到服务器内容00：" + res.data)
            that.setData({
              message: res.data
            })
            //decoder.decode(res.data, that)
          })
        })
        that.setData({
          motto: "服务器连接成功"
        })
      },
      fail() {
        socketOpen = false
        that.setData({
          motto: "服务器连接失败"
        })
      }
    })
  },
  onShow: function () {
    this.showAnimated();
    var t = this;
    setTimeout(function () {
      t.setData({
        bottom: "animated slideInUp"
      })
    }, 2e3), setTimeout(function () {
      t.setData({
        bottom_one: "animated slideInUp"
      })
    }, 2100), setTimeout(function () {
      t.setData({
        bottom_two: "animated slideInUp"
      })
    }, 2200), setTimeout(function () {
      t.setData({
        bottom_three: "animated slideInUp"
      })
    }, 2300), setTimeout(function () {
      t.setData({
        bottom_four: "animated slideInUp"
      })
    }, 2400), setTimeout(function () {
      t.setData({
        bottom_one: "bottom-4s-move"
      })
    }, 3100), setTimeout(function () {
      t.setData({
        bottom_two: "bottom-3s-move"
      })
    }, 3200), setTimeout(function () {
      t.setData({
        bottom_three: "bottom-2s-move"
      })
    }, 3300), setTimeout(function () {
      t.setData({
        bottom_four: "bottom-1s-move"
      })
    }, 3400), setTimeout(function () {
      t.setData({
        bottom_six: "bottom-6s-move"
      })
    }, 3400), setTimeout(function () {
      t.setData({
        bottom_seven: "bottom-4s-move"
      })
    }, 3400)
    if (!socketOpen) {
      this.webSocket()
    }
  },
  onHide: function () {
    this.cleanAnimated(), this.setData({
      bottom: "",
      bottom_one: "",
      bottom_two: "",
      bottom_three: "",
      bottom_four: "",
      bottom_six: ""
    })
  },
  onUnload: function () { },
  onPullDownRefresh: function () { }
});
